//
//  CYLPlusButtonSubclass.swift
//  SkylowAPP
//
//  Created by 王王凤丽 on 2024/9/20.
//

import UIKit
import CYLTabBarController
import zhPopupController
import HXPhotoPicker

class CYLPlusButtonSubclass: CYLPlusButton, CYLPlusButtonSubclassing {
    
    var config: PickerConfiguration = .init()
    
    
    static func plusButton() -> Any! {
        let button = CYLPlusButtonSubclass()
        button.setBackgroundImage(UIImage(named: "Tab_add_Nor"), for: .normal)
        button.titleLabel?.textAlignment = .center
        button.titleLabel?.font = UIFont.systemFont(ofSize: 10)
        button.adjustsImageWhenHighlighted = false
        button.sizeToFit()
        button.addTarget(button, action: #selector(addPostAction), for: .touchUpInside)
        return button
    }

    @objc func addPostAction(){
        self.publishPop.show(in:  (self.viewController?.view)!, duration: 0.25)
       
    }

    static func indexOfPlusButtonInTabBar() -> UInt {
        return 2
    }

    static func multiplier(ofTabBarHeight _: CGFloat) -> CGFloat {
        return 0.3
    }

    static func constantOfPlusButtonCenterYOffset(forTabBarHeight _: CGFloat) -> CGFloat {
        return 0
    }


    static func shouldSelectPlusChildViewController() -> Bool {
        return true
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        // tabbar UI layout setup
//        let imageViewEdgeWidth: CGFloat = bounds.size.width * 0.8
//        let imageViewEdgeHeight: CGFloat = imageViewEdgeWidth * 0.9
//
//        let centerOfView = bounds.size.width * 0.5
//        let labelLineHeight = titleLabel!.font.lineHeight
//        let verticalMargin = (bounds.size.height - labelLineHeight - imageViewEdgeHeight) * 0.5
//
//        let centerOfImageView = verticalMargin + imageViewEdgeHeight * 0.5
//        let centerOfTitleLabel = imageViewEdgeHeight + verticalMargin * 2 + labelLineHeight * 0.5 + 10
//
//        // imageView position layout
//        imageView!.bounds = CGRect(x: 0, y: 0, width: imageViewEdgeWidth + 100, height: imageViewEdgeHeight)
//        imageView!.center = CGPoint(x: centerOfView, y: centerOfImageView)
//
//        // title position layout
//        titleLabel!.bounds = CGRect(x: 0, y: 0, width: bounds.size.width + 100, height: labelLineHeight)
//        titleLabel!.center = CGPoint(x: centerOfView, y: centerOfTitleLabel)
    }
    
    
    private lazy var publishPop: zhPopupController = {
        let publishPop =  zhPopupController.init(view: self.publishPopView, size: CGSizeMake(310, 260))
        publishPop.presentationStyle = .fromBottom
        return publishPop
    }()
    
    private lazy var publishPopView: SLPublishPopView = {
        let publishPopView =  SLPublishPopView.loadFromXib()!

        publishPopView.cancelBlock = { [unowned self] in
            self.publishPop.dismiss()
        }
        
        publishPopView.createPostBlock = { [unowned self] in
            self.publishPop.dismiss()
            let vc =  PickerResultViewController()
            topVC?.navigationController?.pushViewController(vc, animated: true)
            
        }
        
        publishPopView.createCharacterBlock = { [unowned self] in
            self.publishPop.dismiss()
            self.openAvatarPickerController()
        }
        return publishPopView
    }()
    
    @objc func openAvatarPickerController() {
        
        config.selectMode = .single
        config.selectOptions = .photo
        config.photoList.finishSelectionAfterTakingPhoto = true
        config.photoSelectionTapAction = .openEditor
        config.editor.isFixedCropSizeState = true
        config.editor.cropSize.isRoundCrop = true
        config.editor.cropSize.aspectRatios = []
        config.editor.cropSize.isFixedRatio = true
        config.editor.cropSize.isResetToOriginal = false
        config.modalPresentationStyle = .fullScreen
        
        
        config.appearanceStyle = .normal
        config.navigationViewBackgroundColor = UIColor.colorWith(hexString: "#000000")
        config.navigationViewBackgroudDarkColor = UIColor.colorWith(hexString: "#000000")
        
        config.navigationTitleColor = .white
        config.navigationTintColor = .white
        config.statusBarStyle = .lightContent
        config.navigationBarStyle = .black
        
        #if !targetEnvironment(macCatalyst)
        if #available(iOS 14.0, *), ProcessInfo.processInfo.isiOSAppOnMac {

        }else {
            var cameraConfig = CameraConfiguration()
            cameraConfig.editor.isFixedCropSizeState = config.editor.isFixedCropSizeState
            cameraConfig.editor.cropSize.isRoundCrop = config.editor.cropSize.isRoundCrop
            cameraConfig.editor.cropSize.aspectRatios = config.editor.cropSize.aspectRatios
            cameraConfig.editor.cropSize.isFixedRatio = config.editor.cropSize.isFixedRatio
            cameraConfig.editor.cropSize.isResetToOriginal = config.editor.cropSize.isResetToOriginal
            config.photoList.cameraType = .custom(cameraConfig)
        }
        #endif
        
        let vc = PhotoPickerController.init(config: config)
        vc.modalPresentationStyle = .fullScreen
        vc.pickerDelegate = self
        vc.autoDismiss = false
        topVC?.present(vc, animated: true, completion: nil)
      
    }
    
    

//    
}

extension CYLPlusButtonSubclass: PhotoPickerControllerDelegate {
    func pickerController(_ pickerController: PhotoPickerController, didFinishSelection result: PickerResult) {
        pickerController.dismiss(true) {
//            let pickerResultVC = CharacterCreateStepViewController.init()
//            pickerResultVC.config = pickerController.config
//            pickerResultVC.selectedAssets = result.photoAssets
//            pickerResultVC.isOriginal = result.isOriginal
//            topVC?.navigationController?.pushViewController(pickerResultVC, animated: true)
            
            let createVC = CharacterCreateStepViewController()
            let nav = SWNavigationController(rootViewController: createVC)
            nav.modalPresentationStyle = .fullScreen
            topVC?.present(nav, animated: true)
        }
    }
    func pickerController(didCancel pickerController: PhotoPickerController) {
        pickerController.dismiss(animated: true, completion: nil)
    }
}

